from fastapi import APIRouter, Depends, HTTPException, status
from typing import List
from app.models.order import Order, OrderCreate, OrderUpdate, OrderStatus
from app.models.user import User
from app.services.order_service import OrderService, get_order_service
from app.services.user_service import UserService, get_user_service
from app.config.logger import logger

router = APIRouter(
    prefix="/orders",
    tags=["orders"]
)

@router.post("/", response_model=Order)
async def create_order(
    order: OrderCreate,
    current_user: User = Depends(get_user_service().get_current_user),
    order_service: OrderService = Depends(get_order_service)
):
    """创建新订单"""
    # 确保用户只能为自己创建订单
    if order.user_id != current_user.id:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Cannot create order for another user"
        )
    
    try:
        return order_service.create_order(order)
    except HTTPException as e:
        raise e
    except Exception as e:
        logger.error(f"Error creating order: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Could not create order"
        )

@router.get("/me", response_model=List[Order])
async def get_my_orders(
    current_user: User = Depends(get_user_service().get_current_user),
    order_service: OrderService = Depends(get_order_service)
):
    """获取当前用户的订单列表"""
    return order_service.get_user_orders(current_user.id)

@router.get("/{order_id}", response_model=Order)
async def get_order(
    order_id: int,
    current_user: User = Depends(get_user_service().get_current_user),
    order_service: OrderService = Depends(get_order_service)
):
    """获取订单详情"""
    order = order_service.get_order(order_id)
    # 确保用户只能查看自己的订单
    if order.user_id != current_user.id:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Not authorized to view this order"
        )
    return order

@router.put("/{order_id}/status", response_model=Order)
async def update_order_status(
    order_id: int,
    status: OrderStatus,
    current_user: User = Depends(get_user_service().get_current_user),
    order_service: OrderService = Depends(get_order_service)
):
    """更新订单状态"""
    order = order_service.get_order(order_id)
    # 确保用户只能更新自己的订单
    if order.user_id != current_user.id:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Not authorized to update this order"
        )
    return order_service.update_order_status(order_id, status)

@router.post("/{order_id}/cancel", response_model=Order)
async def cancel_order(
    order_id: int,
    current_user: User = Depends(get_user_service().get_current_user),
    order_service: OrderService = Depends(get_order_service)
):
    """取消订单"""
    order = order_service.get_order(order_id)
    # 确保用户只能取消自己的订单
    if order.user_id != current_user.id:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Not authorized to cancel this order"
        )
    return order_service.cancel_order(order_id) 